package cn.layku.web.controller;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import cn.layku.web.config.NoRepeatSubmit;
import cn.layku.web.service.order.OrderService;
import cn.layku.web.util.ApiResult;
import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Map;
import java.util.Random;

@RestController
public class ApiController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private OrderService orderService;

    /**
     * 创建订单
     */
    @PostMapping("/order/create")
    @NoRepeatSubmit
    public Object orderCreate(@RequestBody Map<String, Object> map) {
        Snowflake snowflake = IdUtil.getSnowflake(1, 1);
        map.put("orderNo", snowflake.nextId());
        logger.info("创建订单前端参数->{}", JSON.toJSONString(map));
        ApiResult<Object> orderAdd = orderService.create(map);
        logger.info("创建订单返回参数->{}", JSON.toJSONString(orderAdd));
        return ApiResult.successResult();
    }

    public static void main(String[] args) {
        String[] goods = {"一等奖", "二等奖", "三等奖", "四等奖"};
        int[] power = {1, 20, 50, 1000};
        int sum = Arrays.stream(power).reduce(0, (b, c) -> b + c);
        Random random = new Random();
        int a = 0, b = 0, c = 0, d = 0;
        for (int i = 0; i < 10; i++) {
            int index = random.nextInt(sum);
            if (index == 0) {
                a++;
            } else if (index >= 1 && index < 21) {
                b++;
            } else if (index >= 21 && index < 71) {
                c++;
            } else {
                d++;
            }
            System.out.println(index);
        }

        System.out.println("a:" + a);
        System.out.println("b:" + b);
        System.out.println("c:" + c);
        System.out.println("d:" + d);
    }

}